<?php

/**
 * Flickr Photosets - Photo Page
 * Displays a single photo either in a photoset or the photostream.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 8.5.22
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr
*/
$fp = new FlickrPhotosetsPhoto();

// Turn off tagging for now if this is a video
//if ($fp->photo['media'] == 'video') {
if (true) {
	$fp->config['enable_tagging'] = false;
}

// Retrieve medium size
$photo_size = $fp->get_photo_size();

?>

<?=$fp->get_script('common')?>
<script type="text/javascript">
	// Context
	var callbackUrl = "<?=$fp->callback_url?>";
	var uid = "<?=$fp->uid?>";
	var pid = "<?=$fp->photo['id']?>";
	
	// Photo dimensions
	var photoWidth = <?=$photo_size['width']?>;
	var photoHeight = <?=$photo_size['height']?>;
	
	// Frame dimensions
	var frameWidth = 150;
	var frameHeight = 150;
	var frameBorder = 8;
	
	// Map stuff
	var mapHolder = $('photo_map');
	var photo = $('photo');
	var mapLink = $('maplink');
	var heightElement = $('photo_border');
</script>
<?=$fp->get_script('map')?>

<?php if ($fp->tagging_beta): ?>
<fb:user-agent includes="WebKit">
	<script type="text/javascript">
		var safariAdjust = 2;
	</script>
<fb:else>
	<script type="text/javascript">
		var safariAdjust = 0;
	</script>
</fb:else></fb:user-agent>

<?=$fp->get_script('tagging_fb')?>

<fb:if-is-friends-with-viewer uid="<?=$fp->uid?>">
<?php if ($fp->config['enable_tagging']) {
	
	echo '<script type="text/javascript">';
	echo 'enableTagging();';
	
	echo 'function addTags(event) {';
	$tagged_users = $fp->get_tagged_users($fp->photo['id'], $fp->config['flickr_token']);
	foreach ($tagged_users as $tag) {
		if (is_numeric($tag['tag_content'])) {
			$user = $fp->facebook->api_client->users_getInfo($tag['tag_content'], 'name');
			$tag['tag_content'] = $user[0]['name'];
		}
		if ($tag['complete']) {
			echo 'addTag('.$tag['tag_x'].', '.$tag['tag_y'].', "'.$tag['tag_content'].'", "'.str_replace('-', '', $tag['tag_ids'][0]).'");';
		}
	}
	echo '} </script>';
	
	foreach ($tagged_users as $tag) {
		echo '<fb:js-string var="tags.id', str_replace('-', '', $tag['tag_ids'][0]), '">';
		if (is_numeric($tag['tag_content'])) {
			echo '<span><fb:name uid="', $tag['tag_content'], '" linked="false" useyou="false" /></span>';
		} else {
			echo '<span>', $tag['tag_content'], '</span>';
		}
		echo '</fb:js-string>';
	}
	
} ?>
</fb:if-is-friends-with-viewer>

<fb:js-string var="tagFormContent">
<form id="tag_form" onsubmit="submitTag(); return false;">
	<fb:friend-selector uid="<?=$fp->uid?>" name="tag_content" idname="tag" include_me="true" />
	<p id="tag_form_text">Enter any name or tag.</p>
	<input type="hidden" name="method" value="add_tag" />
	<input type="hidden" name="pid" value="<?=$fp->photo['id']?>" />
	<input type="hidden" name="uid" value="<?=$fp->uid?>" />
	<input type="hidden" name="token" value="<?=hash(FlickrPhotosets::hash_method, $fp->uid.$fp->photo['id'].FACEBOOK_API_SECRET)?>" />
	<input type="hidden" id="tag_y" name="tag_y" value="" />
	<input type="hidden" id="tag_x" name="tag_x" value="" />
	<input type="submit" class="inputbutton" value="Tag" />
	<input type="button" class="inputbutton inputaux" onclick="cancelTag();" value="Cancel" />
</form>
</fb:js-string>

<?php endif; ?>

<fb:title><?=$fp->title?> | <?=$fp->photo['title']?></fb:title>
<?=$fp->get_stylesheet('photo')?>
<?=$fp->get_header($fp->tab, $fp->title)?>

<div id="photo_header" class="clearfix">
	
	<h2 id="photo_title"><?=$fp->photo['title']?>&nbsp;</h2>
	
	<div class="summary">
		Photo <?=$fp->current_photo?> of <?=$fp->photo_count?>
		<span class="pipe">|</span>
		<?php
		// Photo in a photoset
		if ($fp->in_photoset) { ?>
			<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/sets/<?=$fp->photoset['id']?>/page/<?=$fp->page?>/">Back to <?=$fp->photoset['title']?></a>
		<?php }
		// Photo of someone
		else if ($fp->of_user) { ?>
			<a href="<?=$fp->canvas_url?>/photos/of/<?=$fp->of?>/">Back to Photos of <fb:name uid="<?=$fp->of?>" useyou="true" capitalize="true" firstnameonly="true" linked="false" /></a>
		<?php }
		// Photo in the photostream
		else { ?>
			<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/page/<?=$fp->page?>/">Back to Photostream</a>
		<?php }?>
	</div>	
	
	<ul class="pagerpro navlinks">
		<li><a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$fp->context['prevphoto']['id']?><?=$fp->url_append?>/">Previous</a></li>
		<li><a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$fp->context['nextphoto']['id']?><?=$fp->url_append?>/">Next</a></li>
	</ul>
	
</div>

<div id="name_frame" style="display: none;"></div>
<div id="tag_frame" style="display: none;">
	<div id="tag_frame_inside" onclick="return frameClick(event);" />
</div>
<div id="tag_selector" style="display: none;"></div>

<div id="photo_border">
	
	<?php if ($fp->show_geodata) { ?>
		<div id="photo_map" style="opacity: 0; display: none; width: <?=$fp->fb_sizes['canvas']?>; background-image: url(<?=$fp->callback_url?>/images/ajax-loader-large.gif);">&nbsp;</div>
	<?php } ?>
	
	<div id="tag_message" class="message">
		<p>Click on the people in the photo to tag them.<fb:if-is-user uid="<?=$fp->uid?>"><fb:else><fb:name uid="<?=$fp->uid?>" firstnameonly="true" /> will be asked to approve all tags before others can see them.</fb:else></fb:if-is-user></p> 
		<input type="button" class="inputbutton" onclick="stopTagging();" value="Done Tagging" />
		<div class="clearfix"> </div>
	</div>
	
	<?//=$fp->get_no_tag_warning($tag_note_assoc)?>
	
	<div id="photo" >
		<?php if ($fp->photo['media'] == 'video'): ?>
			
			<?php
			// Make the video no wider than 550 px
			$width = $fp->photo['video']['width'];
			$height = $fp->photo['video']['height'];
			$ratio = $width / $height;
			
			$video_width = min(550, $width);
			$video_height = $video_width / $ratio;
			?>
			
			<div class="video">
				<fb:swf swfsrc="http://www.flickr.com/apps/video/stewart.swf?v=1.169" height="<?=$video_height?>" width="<?=$video_width?>" flashvars="intl_lang=en-us&amp;photo_secret=<?=$fp->photo['secret']?>&amp;photo_id=<?=$fp->photo['id']?>" />
			</div>
			
		<?php else: ?>
			
			<div style="position: relative; width: <?=$photo_size['width']+2?>px; height: <?=$photo_size['height']+2?>px; margin: 0 auto;">
				<a href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$fp->context['nextphoto']['id']?><?php if ($fp->in_photoset) { ?>/in/set-<?=$fp->photoset['id']?><?php } ?>/" id="thephoto_link">
					<?php if ($photo['license'] == 0) { ?>
						<img src="<?=$fp->callback_url?>/images/pixel.gif" width="<?=$photo_size['width']+2?>px" height="<?=$photo_size['height']+2?>px" style="position: absolute; top: 0; left: 0; border: 0; background-color: transparent;" />
					<?php } ?>
					<img id="thephoto" src="<?=$fp->flickr->buildPhotoUrl($fp->photo, 'medium')?>" />
				</a>
			</div>
			
		<?php endif; ?>
	</div>
	
	<div id="photo_caption">
		
		<div id="photo_context">
			<?php if ($fp->in_photoset) { ?>
				<div id="main_context">
					From the photoset <a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>sets/<?=$fp->photoset['id']?>/"><?=$fp->photoset['title']?></a> by <a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>"><?=$fp->flickr_user['username']?></a>
				</div>
			<?php }
			// In photostream
			else { ?>
				<div id="main_context">
					From <a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?>"><?=$fp->flickr_user['username']?>'s photostream</a>
				</div>
				
			<?php } ?>
			<?=$fp->get_all_contexts()?>
		</div>
		
		<ul id="photo_meta">
			<li id="photo_description"><?=str_replace("\n", '<br />', $fp->photo['description'])?></li>
			<?php if ($fp->config['enable_tagging'] and $fp->config['flickr_auth'] == 'w') {
				
				if ($tagged_users_string = $fp->get_tagged_users_string($fp->config['facebook_uid'], $fp->photo['id'], $tagged_users)) {
					echo '<li id="photo_tags"><fb:if-is-friends-with-viewer uid="'.$fp->uid.'">', str_replace('{canvasURL}', $fp->canvas_url, str_replace('{callbackURL}', $fp->callback_url, $tagged_users_string)), '</fb:if-is-friends-with-viewer></li>';
				}
				else {
					echo '<li id="photo_tags" class="hide"></li>';
				}
				
			} ?>
			<li id="photo_date"><?=$fp->get_photo_date($fp->photo)?>.</li>
		</ul>
		
		<div class="clearfix"></div>
				
	</div>
	
</div>

<div id="photo_footer">
	
	<div id="side">
		
		<fb:share-button class="meta">
			<meta name="description" content="<?=htmlentities($fp->photo['description'])?>" />
			<link rel="image_src" href="<?=$fp->flickr->buildPhotoUrl($fp->photo, 'thumbnail')?>" />
			<link rel="target_url" href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$fp->photo['id']?>/" />
		<?php if ($fp->photo['media'] == 'video') { ?>
			<meta name="medium" content="video" />
			<meta name="title" content="Flickr Video from <?=$fp->flickr_user['username']?>: <?=$fp->photo['title']?>" />
			<link rel="video_src" href="http://www.flickr.com/apps/video/stewart.swf?v=1.169&intl_lang=en-us&photo_secret=<?=$fp->photo['secret']?>&photo_id=<?=$fp->photo['id']?>" />
			<meta name="video_height" content="<?=$video_height?>" />
			<meta name="video_width" content="<?=$video_width?>" />
			<meta name="video_type" content="application/x-shockwave-flash" />
		<?php } else { ?>
			<meta name="medium" content="photo" />
			<meta name="title" content="Flickr Photo from <?=$fp->flickr_user['username']?>: <?=$fp->photo['title']?>" />
		<?php } ?>
		</fb:share-button>
		
		<div id="photo_actions">
			
			<?php if ($fp->tagging_beta): ?>
			<?php
			/*
			If tagging isn't enabled
			*/
			if (!$fp->config['enable_tagging']) { ?>
				
				<fb:if-is-user uid="<?=$fp->uid?>">
					<fb:dialog id="enable_tagging">
						<fb:dialog-title>You need to enable tagging first!</fb:dialog-title>	
						<fb:dialog-content><p>Would you like to enable tagging?</p>
							<form id="tag_form">
								<input type="hidden" name="enable_tagging" value="1" />
								<input type="hidden" name="token" value="<?=md5($fp->uid.FACEBOOK_API_SECRET)?>" />
							</form>
						</fb:dialog-content>
						<fb:dialog-button type="submit" value="Enable Tagging" clickrewriteurl="<?=$fp->callback_url?>/tag_photo.php?uid=<?=$fp->uid?>&redir=<?=$_SERVER['SCRIPT_URI']?>" clickrewriteid="enable_tagging" clickrewriteform="tag_form" />
						<fb:dialog-button type="button" value="Cancel" close_dialog="1" />
					</fb:dialog>
					<div id="tag_photo"></div>
					<span class="anchor" clicktoshowdialog="enable_tagging">Tag This Photo</span>
				</fb:if-is-user>
				
			<?php
			/*
			If the user needs to grant write permissions
			*/
			} else if ($fp->config['flickr_auth'] != 'w') { ?>
				
				<fb:if-is-user uid="<?=$fp->uid?>">
					<fb:dialog id="give_write_permissions">
						<fb:dialog-title>Give Write Permissions</fb:dialog-title>	
						<fb:dialog-content>
							<p>You will need to give write permissions to <?=$fp->application_name?> before you can enable tagging.</p>
							<p><strong>Write permissions</strong> allow <?=$fp->application_name?> to access your photos, edit your photo information, and upload photos to your account.</p>
							<p>After you authenticate with Flickr, you will be redirected to the configuration page where you can enable tagging.</p>
						</fb:dialog-content>
						<fb:dialog-button type="button" value="Authorize Write Permissions" href="<?=$fp->callback_url?>/auth.php?write" />
						<fb:dialog-button type="button" value="Cancel" close_dialog="1" />
					</fb:dialog>
					<span class="anchor" clicktoshowdialog="give_write_permissions">Tag This Photo</span>
				</fb:if-is-user>
				
			<?php
			/*
			Everything is good!
			*/
			} else { ?>
				
				<fb:if-is-friends-with-viewer uid="<?=$fp->uid?>" includeself="true">
					<fb:user-agent includes="ie 6">
						<fb:dialog id="unsupported_browser">
							<fb:dialog-title>Unsupported Browser</fb:dialog-title>	
							<fb:dialog-content>
								<p>You are currently using Internet Explorer 6. There are some bugs in the tagging feature that may cause it to not work properly.</p>
								<p>Please <a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">upgrade to Internet Explorer 7</a> or switch to <a href="http://getfirefox.com">Firefox</a> or <a href="http://www.apple.com/safari/">Safari</a> to ensure that the feature will always work correctly.</p>
							</fb:dialog-content>
							<fb:dialog-button type="button" value="Okay" close_dialog="1" />
						</fb:dialog>
						<a href="#" clicktoshowdialog="unsupported_browser" onclick="startTagging();">Tag This Photo</a>
					</fb:user-agent>
					<fb:user-agent excludes="ie 6">
						<span class="anchor" onclick="startTagging();">Tag This Photo</span>
					</fb:user-agent>
				</fb:if-is-friends-with-viewer>
				
			<?php } ?>
			<?php endif; ?>
			
			<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?><?=$fp->photo['id']?>/">Flickr Photo Page</a>
			<a href="<?=$fp->flickr->urls_getUserPhotos($fp->nsid)?><?=$fp->photo['id']?>/#reply">Flickr Comments</a>
			<?php if ($fp->show_geodata) { 
				echo '<a href="'.$fp->flickr->urls_getUserPhotos($fp->nsid).$fp->photo['id'].'/map/">Flickr Map</a>';
			} ?>
			
			<?php if ($fp->beta): ?>
				<fb:dialog id="profile_pic">
					<fb:dialog-title>Make <?=$fp->photo['title']?> your Profile Picture</fb:dialog-title>
					<fb:dialog-content>
						<p>Not yet.</p>
						<fb:prompt-permission perms="photo_upload"></fb:prompt-permission>
					</fb:dialog-content>
					<fb:dialog-button type="button" value="Okay" close_dialog="1" />
				</fb:dialog>
				<a href="#" clicktoshowdialog="profile_pic">Make Profile Picture</a>
			<?php endif; ?>
			
		</div>
	</div>
	
	<ul id="photo_info">
		
		<li id="license"><?=$fp->license?></li>
		
		<?php
		/*
		Tags
		*/
		$tag_string = $fp->get_tag_string($fp->photo);
		if (!empty($tag_string)) { ?>
			<li id="tags">Tags: <?=$tag_string?>.</li>
		<?php } ?>
		
		<?php
		/*
		Camera
		*/
		$camera_string = $fp->get_camera_string($fp->exif_info, true);
		if (!empty($camera_string)) { ?>
			<li id="camera">Taken with <?=$camera_string?>.</li>
		<?php } ?>
		
		<?php
		/*
		Location
		*/
		if ($fp->show_geodata) { ?>
			<li id="geo"><?=($fp->photo['media'] == 'video') ? 'Filmed' : 'Taken' ?> in <?=$fp->get_geo_string($fp->geo)?> (<a id="maplink" href="<?=$fp->canvas_url?>/photos/<?=$fp->uid?>/<?=$fp->photo['id']?>/map/">see on a map</a>).</li>
		<?php } ?>
		
		<?php
		/*
		People who count the photo as a favorite
		*/
		if ($fp->favorite['total'] > 0) { ?>
			<li id="favorite"><span class="anchor" onclick="blind('favorite_people'); return false;"><?=$fp->favorite['total']?> <?=($fp->favorite['total'] == 1) ? 'person' : 'people' ?></span> <?=($fp->favorite['total'] == 1) ? 'calls' : 'call' ?> this photo a favorite.
				<div id="favorite_people" style="display: none;">
					<?=$fp->get_favorited_list()?>
				</div>
			</li>
		<?php } ?>
		
	</ul>
	
	<div id="photo_comments">
		<?php if ($fp->config['enable_comments']) { ?>
			<div class="facebook">
				<fb:comments xid="flickrphotosetsphoto_<?=$fp->photo['id']?>" canpost="true" candelete="<?=($fp->uid == $fp->viewer_uid) ? 'true' : 'false' ?>" showform="true" send_notification_uid="<?=$fp->uid?>">
					<fb:title>Comments on <?=(!empty($fp->photo['title'])) ? $fp->photo['title'] : $fp->photo['id'] ?></fb:title>
				</fb:comments>
			</div>
		<?php } ?>
		<div class="flickr">
			<?=$fp->print_comments($fp->flickr->photos_comments_getList($fp->photo['id']))?>
			<div class="clearfix"></div>
		</div>
	</div>
	
</div>

<div class="clearfix"></div>

<?=$fp->echo_footer(true)?>